﻿
    using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Windows;
namespace WCYCReader.Utility
{
    public class CodeFormat
    {
        public static string BigEndianUnicodeByteToUnicodeString(byte[] buf)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(Encoding.BigEndianUnicode.GetString(buf, 0, buf.Length));
            return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
        }

        public string CleanInvalidXmlChars(string text)
        {
            string pattern = @"[^\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF\u4e00-\u9fa5]";
            return Regex.Replace(text, pattern, "");
        }

        public static string Gb2312ByteToUnicodeString(byte[] buf)
        {
            Gb2312Encoding encoding = new Gb2312Encoding();
            byte[] bytes = Encoding.Unicode.GetBytes(encoding.GetString(buf, 0, buf.Length));
            return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
        }

        public static string GetNoBomType(byte[] buf, int len)
        {
            int index = 0;
            while (index < len)
            {
                byte num1 = buf[index];
                if (((buf.Length >= (index + 2)) && ((buf[index] & 0xe0) == 0xc0)) && ((buf[index + 1] & 0xc0) == 0x80))
                {
                    return UTF8ByteToUnicodeString(buf);
                }
                if (((buf.Length >= (index + 3)) && ((buf[index] & 240) == 0xe0)) && (((buf[index + 1] & 0xc0) == 0x80) && ((buf[index + 2] & 0xc0) == 0x80)))
                {
                    return UTF8ByteToUnicodeString(buf);
                }
                if ((((buf.Length >= (index + 4)) && ((buf[index] & 0xf8) == 240)) && (((buf[index + 1] & 0xc0) == 0x80) && ((buf[index + 2] & 0xc0) == 0x80))) && ((buf[index + 3] & 0xc0) == 0x80))
                {
                    return UTF8ByteToUnicodeString(buf);
                }
                if ((((buf.Length >= (index + 5)) && ((buf[index] & 0xfc) == 0xf8)) && (((buf[index + 1] & 0xc0) == 0x80) && ((buf[index + 2] & 0xc0) == 0x80))) && (((buf[index + 3] & 0xc0) == 0x80) && ((buf[index + 4] & 0xc0) == 0x80)))
                {
                    return UTF8ByteToUnicodeString(buf);
                }
                if ((((buf.Length >= (index + 6)) && ((buf[index] & 0xfe) == 0xfc)) && (((buf[index + 1] & 0xc0) == 0x80) && ((buf[index + 2] & 0xc0) == 0x80))) && ((((buf[index + 3] & 0xc0) == 0x80) && ((buf[index + 4] & 0xc0) == 0x80)) && ((buf[index + 5] & 0xc0) == 0x80)))
                {
                    return UTF8ByteToUnicodeString(buf);
                }
                return Gb2312ByteToUnicodeString(buf);
            }
            return Gb2312ByteToUnicodeString(buf);
        }

        public static string GetTransformString(Stream stream)
        {
            BinaryReader reader = new BinaryReader(stream);
            byte[] buf = reader.ReadBytes((int) stream.Length);
            reader.Close();
            if ((buf == null) || (buf.Length <= 0))
            {
                return null;
            }
            if (((buf.Length >= 3) && (buf[0] == 0xef)) && ((buf[1] == 0xbb) && (buf[2] == 0xbf)))
            {
                return UTF8ByteToUnicodeString(buf);
            }
            if (((buf.Length >= 2) && (buf[0] == 0xfe)) && (buf[1] == 0xff))
            {
                return BigEndianUnicodeByteToUnicodeString(buf);
            }
            if (((buf.Length < 2) || (buf[0] != 0xff)) || (buf[1] != 0xfe))
            {
                return GetNoBomType(buf, buf.Length);
            }
            if (((buf.Length >= 4) && (buf[2] == 0)) && (buf[3] == 0))
            {
                return null;
            }
            return UnicodeByteToUnicodeString(buf);
        }

        public static string GetTransformString(string FILE_NAME)
        {
            return GetTransformString(Application.GetResourceStream(new Uri(FILE_NAME, UriKind.Relative)).Stream);
        }

        public static string UnicodeByteToUnicodeString(byte[] buf)
        {
            return Encoding.Unicode.GetString(buf, 0, buf.Length);
        }

        public static string UTF8ByteToUnicodeString(byte[] buf)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(Encoding.UTF8.GetString(buf, 0, buf.Length));
            return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
        }
    }
}

